package com.ruoyi.system.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Research;
import com.ruoyi.system.domain.vo.appUserVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AppUser;
import com.ruoyi.system.service.IAppUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户，存储用户基本信息。Controller
 *
 * @author ruoyi
 * @date 2024-06-25
 */
@RestController
@RequestMapping("/appUser/appUser")
public class AppUserController extends BaseController
{
    @Autowired
    private IAppUserService userService;

    /**
     * 查询用户，存储用户基本信息。列表
     */
    @ApiOperation("获取用户列表")
    @PreAuthorize("@ss.hasPermi('appUser:appUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppUser user)
    {
        startPage();
        List<AppUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户，存储用户基本信息。列表
     */
    @ApiOperation("导出用户，存储用户基本信息。列表")
    @PreAuthorize("@ss.hasPermi('appUser:appUser:export')")
    @Log(title = "用户，存储用户基本信息。", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppUser user)
    {
        List<AppUser> list = userService.selectUserList(user);
        ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
        util.exportExcel(response, list, "用户，存储用户基本信息。数据");
    }

    /**
     * 获取用户，存储用户基本信息。详细信息
     */
    @ApiOperation("获取用户，存储用户基本信息。详细信息")
    @PreAuthorize("@ss.hasPermi('appUser:appUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户，存储用户基本信息。
     */
    @ApiOperation("新增用户，存储用户基本信息。")
    @PreAuthorize("@ss.hasPermi('appUser:appUser:add')")
    @Log(title = "用户，存储用户基本信息。", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppUser user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户，存储用户基本信息。
     */
    @ApiOperation("修改用户，存储用户基本信息。")
    @PreAuthorize("@ss.hasPermi('appUser:appUser:edit')")
    @Log(title = "用户，存储用户基本信息。", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppUser user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户，存储用户基本信息。
     */
    @ApiOperation("删除用户，存储用户基本信息。")
    @PreAuthorize("@ss.hasPermi('appUser:appUser:remove')")
    @Log(title = "用户，存储用户基本信息。", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }

    /**
     * 修改状态
     */
    @ApiOperation("修改状态")
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody AppUser user)
    {
        return toAjax(userService.updateAppUserStatus(user));
    }

    /**
     * 查询医院对应得项目
     */
    @ApiOperation("查询医院对应得项目")
    @GetMapping("/res")
    public List<Research> resByHospId(int id){
        return userService.resByHospId(id);
    }

    /**
     * 最新版本得新增小程序用户
     */
    @ApiOperation("最新版本得新增小程序用户")
    @PostMapping("/addApp")
    public int insertNewApp(@RequestBody appUserVo app){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(formattedDateTime);
            app.setCreateTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return userService.insertNewApp(app);
    }

    /**
     * 进行表关联
     */
    @ApiOperation("进行表关联")
    @PostMapping("addRu")
    public int insertRu(@RequestBody appUserVo app){
        return userService.insertRu(app);
    }

    /**
     * 最新版本得修改小程序用户
     */
    @ApiOperation("最新版本得修改小程序用户")
    @PutMapping("/updateNewApp")
    public int updateNewApp(@RequestBody appUserVo app){
        return userService.updateNewApp(app);
    }

    /**
     * 进行关联修改
     */
    @ApiOperation("进行关联修改")
    @PutMapping("/updateRu")
    public int updateRu(@RequestBody appUserVo app){
        return userService.updateRu(app);
    }
}
